﻿using Microsoft.AspNetCore.Mvc;
using PlatformNetCore.Common;
using PlatformNetCore.Common.Helper;
using PlatformNetCore.Entity;
using PlatformNetCore.IService;
using PlatformNetCore.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace PlatformNetCore.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class BasisController : BaseController
    {
        private readonly IBasisService basisService;
        private readonly IMenuService menuService;
        private readonly ISmtpConfigService smtpConfigService;
        private readonly ISmtpTemplateService smtpTemplateService;
        private readonly ISinglePageService singlePageService;
        private readonly ICompanyService companyService;
        public BasisController(IBasisService _basisService, ISmtpConfigService _smtpConfigService, ISmtpTemplateService _smtpTemplateService
            , ISinglePageService _singlePageService, ICompanyService _companyService, IMenuService _menuService)
        {
            basisService = _basisService;
            smtpConfigService = _smtpConfigService;
            smtpTemplateService = _smtpTemplateService;
            singlePageService = _singlePageService;
            companyService = _companyService;
            menuService = _menuService;
        }

        #region 分类管理
        [RoleAuthorize("CategoryList")]
        public IActionResult CategoryList(string search = "",string categoryType = "", int pageindex = 1, int pagesize = 10)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<Category, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.CategoryName.Contains(search));
                    }
                    if (!string.IsNullOrWhiteSpace(categoryType))
                    {
                        predicate = predicate.And(k => k.CategoryType == categoryType);
                    }
                    return Json(basisService.GetCategoryPagedList(pageindex, pagesize, predicate));
                }
                ViewBag.CategoryTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(CategoryType),true);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("CategoryAdd")]
        public IActionResult CategoryAdd()
        {
            try
            {
                Category model = new Category();
                ViewBag.CategoryTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(CategoryType));
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("CategoryAdd")]
        public IActionResult CategoryAdd(Category model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(basisService.AddCategory(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("CategoryDel")]
        public IActionResult CategoryDel(string id)
        {
            try
            {
                return Json(basisService.DeleteCategory(id));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("CategoryEdit")]
        public IActionResult CategoryEdit(string id)
        {
            try
            {
                ResultResponse<Category> result = basisService.GetCategory(id);
                ViewBag.CategoryTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(CategoryType));
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("CategoryEdit")]
        public IActionResult CategoryEdit(Category model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(basisService.UpdateCategory(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

        #region 邮件服务器配置
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("SmtpConfig")]
        public IActionResult SmtpConfig()
        {
            try
            {
                ResultResponse<SmtpConfig> result = smtpConfigService.GetSmtpConfig();
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("SmtpConfig")]
        public IActionResult SmtpConfig(SmtpConfig model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(smtpConfigService.SaveSmtpConfig(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

        #region 邮件模板管理
        [RoleAuthorize("TemplateList")]
        public IActionResult TemplateList(string search = "", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<SmtpTemplate, bool>> predicate = s => s.ID != "";
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    return Json(smtpTemplateService.GetSmtpTemplatePagedList(pageindex, pagesize, predicate));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("TemplateAdd")]
        public IActionResult TemplateAdd()
        {
            try
            {
                SmtpTemplate model = new SmtpTemplate();
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("TemplateAdd")]
        public IActionResult TemplateAdd(SmtpTemplate model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(smtpTemplateService.AddSmtpTemplate(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("TemplateDel")]
        public IActionResult TemplateDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(smtpTemplateService.UpdateSmtpTemplateState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("TemplateEdit")]
        public IActionResult TemplateEdit(string id)
        {
            try
            {
                ResultResponse<SmtpTemplate> result = smtpTemplateService.GetSmtpTemplate(id);
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("TemplateEdit")]
        public IActionResult TemplateEdit(SmtpTemplate model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(smtpTemplateService.UpdateSmtpTemplate(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

        #region 单页管理
        [RoleAuthorize("SinglePageList")]
        public IActionResult SinglePageList(string search = "", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<SinglePage, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    return Json(singlePageService.GetSinglePagePagedList(pageindex, pagesize, predicate));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("SinglePageAdd")]
        public IActionResult SinglePageAdd()
        {
            try
            {
                SinglePage model = new SinglePage();
                ViewBag.SinglePageTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(SinglePageType));
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("SinglePageAdd")]
        public IActionResult SinglePageAdd(SinglePage model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(singlePageService.AddSinglePage(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("SinglePageDel")]
        public IActionResult SinglePageDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(singlePageService.UpdateSinglePageState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("SinglePageEdit")]
        public IActionResult SinglePageEdit(string id)
        {
            try
            {
                ResultResponse<SinglePage> result = singlePageService.GetSinglePage(id);
                ViewBag.SinglePageTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(SinglePageType));
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("SinglePageEdit")]
        public IActionResult SinglePageEdit(SinglePage model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(singlePageService.UpdateSinglePage(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

        #region 基本资料
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("Company")]
        public IActionResult Company()
        {
            try
            {
                ResultResponse<Company> result = companyService.GetCompany();
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("Company")]
        public IActionResult Company(Company model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(companyService.SaveCompany(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion
        
        #region 导航栏目
        [RoleAuthorize("MenuList")]
        public IActionResult MenuList(string search = "",string menuType = "")
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<Menu, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    if (!string.IsNullOrWhiteSpace(menuType))
                    {
                        predicate = predicate.And(k => k.MenuType == menuType);
                    }
                    return Json(menuService.GetMenuPagedList(1, 500, predicate));
                }
                ViewBag.MenuTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(MenuType),true);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("MenuAdd")]
        public IActionResult MenuAdd()
        {
            try
            {
                Menu model = new Menu();
                ViewBag.MenuTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(MenuType));
                ViewBag.MenuListDLL = menuService.GetMenuListDLL(true);
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("MenuAdd")]
        public IActionResult MenuAdd(Menu model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(menuService.AddMenu(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("MenuDel")]
        public IActionResult MenuDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(menuService.UpdateMenuState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("MenuEdit")]
        public IActionResult MenuEdit(string id)
        {
            try
            {
                ResultResponse<Menu> result = menuService.GetMenu(id);
                ViewBag.MenuTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(MenuType));
                ViewBag.MenuListDLL = menuService.GetMenuListDLL(true);
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("MenuEdit")]
        public IActionResult MenuEdit(Menu model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(menuService.UpdateMenu(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion
    }
}
